package org.spring.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.domain.AdminUser;
import org.spring.springboot.service.UserService;
import org.spring.springboot.util.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * 描述：用户Controller
 * <p>
 * Author: 赵新国
 * Date: 2017/10/12 16:35
 */
@RestController
@RequestMapping("/admin")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 后台用户管理
     *
     * @return
     */
    @RequestMapping(value = "/user")
    public ModelAndView user() {
        return new ModelAndView("admin/system/user");
    }

    /**
     * 获取管理后台用户列表
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getUserList")
    public PageInfo<AdminUser> getUserList(@RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<AdminUser> userList = userService.getUserList();
        logger.info("userList: " + JSON.toJSONString(userList));
        return new PageInfo<AdminUser>(userList);
    }

    /**
     * 管理用户信息页面
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/userForm")
    public ModelAndView userForm(@RequestParam(required = false) Short userId) {
        ModelAndView mav = new ModelAndView("/admin/system/userForm");
        AdminUser user = new AdminUser();
        if (userId != null) {
            user = userService.findAdminUser(userId);
        }
        logger.info("user: " + JSON.toJSONString(user));
        mav.addObject("user", user);
        return mav;
    }

    /**
     * 保存/修改用户信息
     *
     * @param adminUser
     * @return
     */
    @PostMapping(value = "saveUser")
    public HashMap<String, Object> saveUser(@ModelAttribute AdminUser adminUser) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            userService.saveUser(adminUser);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 检查用户名是否存在
     *
     * @param userName
     * @return
     */
    @GetMapping(value = "checkUserName")
    public Boolean checkUserName(@RequestParam Short userId, @RequestParam String userName) {
        AdminUser user = userService.findByUsername(userName);
        if (userId != null) {
            AdminUser adminUser = userService.findAdminUser(userId);
            return adminUser.getUserName().equals(userName) || user == null;
        } else {
            return user == null;
        }
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @PostMapping(value = "deleteUser")
    public HashMap<String, Object> deleteUser(@RequestParam Short userId) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            userService.deleteUser(userId);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

}
